package com.map.core;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

import com.map.brush.PointBrush;
import com.map.event.MapListener;

public final class MapView extends GLSurfaceView {


    private MapRender mapRender;
    private MapListener mapListener;//地图事件控制器

    public MapView(Context context) {
        super(context);
        init();
    }

    public MapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setEGLContextClientVersion(2);
        mapRender = new MapRender();
        setRenderer(mapRender);
        //设置在创建着色器和主动请求刷新的时候进行绘制
        setRenderMode(RENDERMODE_WHEN_DIRTY);
        initTouch();
    }

    /**
     * 初始化地图的触控与监听
     */
    private void initTouch() {
        mapListener = new MapListener(this);
        setOnTouchListener(mapListener);
    }

    /**
     * 刷新绘制
     */
    public void refresh() {
        requestRender();
    }

    /**
     * 绘制点
     */
    public void drawPoints() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                float[] points = {
                        0f, -0.25f, 0f, 0f, 1f, 200f,
                        0f, 0.25f, 1f, 0f, 0f, 100f
                };
                PointBrush pointBrush = new PointBrush(points);
                mapRender.addBrush(pointBrush);
            }
        });
        refresh();
    }

    /**
     * 获取地图渲染器
     * @return 渲染器
     */
    public MapRender getRender(){
        return mapRender;
    }


}
